package com.yanqu.road.server.logic.platform;

import com.yanqu.road.entity.enums.eDeductIngotsStatus;
import com.yanqu.road.entity.platform.PlatformDeductIngotsOrder;
import com.yanqu.road.entity.player.PlayerState;
import com.yanqu.road.logic.bussiness.platform.DeductIngotBussiness;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.recharge.RechargeProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.DeductIngotsModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.task.GameCommandTask;
import com.yanqu.road.utils.YanQuMessageUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class DeductIngotsManager {

    private static Logger logger = LogManager.getLogger(DeductIngotsManager.class.getName());

    /**
     * 扣除元宝发往玩家消息队列
     */
    public static int msgNotifyDeductIngots(String orderNo) {
        PlatformDeductIngotsOrder deductIngotsOrder = DeductIngotBussiness.getPlatformDeductIngotsOrder(orderNo);
        if (null == deductIngotsOrder) {
            logger.error("DeductIngots订单号不存在{}", orderNo);
            return 1;
        }
        // 判断订单状态是否正确
        if (deductIngotsOrder.getStatus() == eDeductIngotsStatus.AlreadyDeduct.getValue()) {
            logger.error("DeductIngots订单已经执行扣除过了{}", orderNo);
            return 2;
        }

        long userId = deductIngotsOrder.getUserId();
        RechargeProto.DeductIngotsMsg.Builder reqMsg = RechargeProto.DeductIngotsMsg.newBuilder();
        reqMsg.setOrderNo(orderNo);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessageByte(Protocol.S_DEDUCT_INGOTS, userId, reqMsg.build().toByteArray());

        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        GameCommandTask cmdWrapper = new GameCommandTask(null, pbMsg, player);
        if (player != null) {
            /* 已登录玩家，提交到玩家指令执行队列 */
            player.setState(PlayerState.ONLINE);
            player.getCommandQueue().add(cmdWrapper);
        } else {
            logger.info("DeductIngots订单玩家{}不在线{}", userId, orderNo);
        }
        return 0;
    }

    /**
     * 通知支付成功，游戏内到账处理
     */
    public static int notifyPlayerDeductIngots(String orderNo) {
        PlatformDeductIngotsOrder deductIngotsOrder = DeductIngotBussiness.getPlatformDeductIngotsOrder(orderNo);
        if (null == deductIngotsOrder) {
            logger.error("DeductIngots订单号不存在{}", orderNo);
            return 1;
        }
        // 判断订单状态是否正确
        if (deductIngotsOrder.getStatus() == eDeductIngotsStatus.AlreadyDeduct.getValue()) {
            logger.error("DeductIngots订单已经执行扣除过了{}", orderNo);
            return 2;
        }

        // 获取玩家
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(deductIngotsOrder.getUserId());
        if (null == player) {
            logger.warn("订单扣除元宝玩家不在线{}", orderNo);
            return 0;
        } else {// 更改订单状态
            return player.getModule(DeductIngotsModule.class).doDeductIngots(deductIngotsOrder, true);
        }
    }
}
